﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MyHelper;

using clf.weighing.entity;


namespace weighing
{
    public class CarDao : BaseDao<CarInfo>
    {
        #region instance

        private static CarDao? instance;
        public static CarDao Instance => instance ??= new CarDao();

        public override string GetTableName()
        {
            return TableName.car_info.ToString();
        }
        #endregion

        public List<CarInfo> SelectByLike(String part)
        {
            if (part.IsNullOrEmpty()) return null;
            string condition = $" {CarFiled.car_number} like '%{part}%'";
            if(App.Config.WeighingConfig.CarLimit == 1)
            {
                condition += $"and {CarFiled.is_self} =1";
            }
            String sql = SqlBuilder.GetSelectSqlWithSoftDeleteCondition(GetTableName(), null, condition);
            var data = Sqlite.Select<CarInfo>(sql);
            return data;
        }

        public bool IsExist(string palte)
        {
            String condition = CarFiled.car_number.ToString() + $" = '{palte}'";
            String sql = SqlBuilder.GetSelectSqlWithSoftDeleteCondition(GetTableName(), "id", condition, null, null, null, 1, 0);
            var data = Sqlite.Select<CarInfo>(sql);
            return data.IsNotNullOrEmpty();
        }

        public int Delete(CarInfo? car, Boolean isTrue = false)
        {
            if (car == null)
            {
                return 0;
            }
            if (WeighingBillDao.Instance.IsCarHasBill(car.carNumber))
            {
                return -1;
            }
            if (isTrue)
            {
                return Sqlite.Delete(car, isTrue);
            }
            else
            {
                String condition = CompanyField.id.ToString() + $" = {car.id}";
                String time = DateTimeHelper.getCurrentDateTime();
                String set = CompanyField.update_time.ToString() + $" = '{time}'"
                        + $", {CompanyField.is_delete} = 1 "
                        + $", {CompanyField.delete_time} = '{time}'"
                        + $", {CompanyField.update_user_name} = '{App.CurrentUser?.name??""}'"
                        + $", {CompanyField.update_user_id} = {App.CurrentUser?.id??0}";
                String sql = SqlBuilder.GetUpdateSql(GetTableName(), set, condition);
                return Sqlite.Delete(sql);
            }

        }
     

        public int InsertOrUpdate(CarInfo car)
        {
            if (IsExist(car.carNumber))
            {
                return Sqlite.Update(car);
            }
            else
            {
                car.addTime = DateTime.Now;
                car.updateTime = (DateTime)car.addTime;
                car.status = 1;
                car.remark = "系统共享";
                return Sqlite.Insert(car);
            }
        }

        public int UpdateByCarNumber(CarInfo car)
        {
            CarInfo temp = FindByCarNumber(car.carNumber);
            car.updateTime = DateTime.Now;
            car.id = temp.id;
            return Sqlite.Update(car);
        }
        public CarInfo FindByIcNumber(uint icCardNo)
        {
            string condition = $"{CarFiled.ic_number} = '{icCardNo}' and {CarFiled.is_delete} = 0";
            string sql = SqlBuilder.GetSelectSql(GetTableName(), null, condition, null, null, null, 1);
            var data = Sqlite.Select<CarInfo>(sql);
            return data.Count > 0 ? data[0] : null;
        }

        public CarInfo FindByCarNumber(string carNumber)
        {
            string condition = $"{CarFiled.car_number} ='{carNumber}' and {CarFiled.is_delete} = 0";
            string sql = SqlBuilder.GetSelectSql(GetTableName(), null, condition, null, null, null, 1);
            var data = Sqlite.Select<CarInfo>(sql);
            return data.Count > 0 ? data[0] : null;
        }
        /// <summary>
        /// 过磅时 保存车辆信息
        /// </summary>
        public void AutoSaveCarInfo(CarInfo? carInfo)
        {
            if (carInfo == null) return;

            if (!IsExist(carInfo.carNumber))
            {
                int row = Sqlite.Insert(carInfo);               
            }
        }

        public int ChaneAllReadyToWait()
        {
            string condition = $"{CarQueueField.status} = {(int)CarQueueStatus.Ready} and {CarQueueField.is_delete} = 0 ";
            string sets = $"{CarQueueField.status} = {(int)CarQueueStatus.Waiting}";
            string sql = SqlBuilder.GetUpdateSql(GetTableName(), sets, condition);
            return Sqlite.Update(sql);
        }

        public int CountAll(string key = "")
        {
            string condition = $"{CarFiled.car_number} like '%{key}%' and {CarFiled.is_delete} = 0 ";           
            string sql = SqlBuilder.GetSelectSql(GetTableName(),$"{CarFiled.id}",condition);
            var data = Sqlite.Select<CarInfo>(sql);
            return data != null ? data.Count : 0;
        }

        public List<CarInfo> SelectPage(int pageindex = 1,int size = 20, string key = "")
        {
            string condition = $"{CarFiled.car_number} like '%{key}%' and {CarQueueField.is_delete} = 0 ";
            int limit = size;
            int offsert = (pageindex - 1) * limit;
            string sql = SqlBuilder.GetSelectSql(GetTableName(), null, condition,null,null,null, limit, offsert);
            var data = Sqlite.Select<CarInfo>(sql);
            return data;
        }

        public List<CarInfo> GetList()
        {
            String sql = SqlBuilder.GetSelectSqlWithSoftDeleteCondition(GetTableName(), null, null);
            var data = Sqlite.Select<CarInfo>(sql);
            return data;
        }

    }
}
